package yxy.game.pm2.command.friend;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.friend.FriendModule;

/** 好友 拒绝申请 */
@MessageType(proto = Command.FriendApplicantRefuse, classtype = FriendMessage.FriendApplicantRefuseRequest.class)
public class CMD_FriendApplicantRefuse extends Command<FriendMessage.FriendApplicantRefuseRequest> {

	@Override
	public void execute(MessageSource messageSource, FriendMessage.FriendApplicantRefuseRequest request) {
		String playerCode = messageSource.getTag();
		String applyCode = request.getPlayerCode();

		FriendMessage.FriendApplicantRefuseResponse.Builder response = FriendMessage.FriendApplicantRefuseResponse.newBuilder();

		FriendModule friendModule = FriendModule.get(playerCode);
		try {
			if (friendModule.tryLock()) {
				friendModule.getApply().remove(applyCode);
				friendModule.set();

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			friendModule.unlock();
		}
	}

	private void send(MessageSource messageSource, FriendMessage.FriendApplicantRefuseResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}